/****************************************************************************
 * Chariott - Continuous Integration Testing Tool
 * Zaid Mohsin
 * The University of York
 * 2012/2013
 * MSc in Information Technology
 * All rights reserved.
 ****************************************************************************/
package chariott.JUnit;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.MultiHashMap;
import org.apache.commons.collections.MultiMap;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import chariott.partition.BinPackingOrder_FF;
import chariott.partition.BinPackingOrder_FF_DEC;
import chariott.partition.LeastUsedOrder;
import chariott.partition.RoundRobinOrder;
import chariott.testing.RunItem;
import chariott.testing.TestCase;

/**
 * JUnit test suite to tests the partionining algorithms in chariott.partition
 * package.
 * 
 * @author Zaid
 * 
 */
public class AlgorithmsTest {

	private ArrayList myList;

	@Before
	public void setUp() throws Exception {
		// create the TestCase objects
		TestCase myCase1 = new TestCase("test2", 2.0);
		TestCase myCase2 = new TestCase("test1", 3.0);
		myList = new ArrayList<RunItem>();
		myList.add(myCase1);
		myList.add(myCase2);
	}

	// return the list to its initial state as declare in setUp();
	@After
	public void tearDown() throws Exception {
		myList = null;
		setUp();
	}

	// test number of item containers if it matches the number of nodes for
	// Round robin algorithm
	@Test
	public void RRcheckNumberOfBuckets() {
		int numOfNodes = 2;

		RoundRobinOrder rr = new RoundRobinOrder(numOfNodes, myList);

		// check if algorithm returns the right number of containers.
		assertTrue(rr.generateRuns().keySet().size() == numOfNodes);
	}

	// test number of item containers if it matches the number of nodes for
	// least used algorithm
	@Test
	public void LUcheckNumberOfBuckets() {
		int numOfNodes = 2;

		LeastUsedOrder lu = new LeastUsedOrder(numOfNodes, myList);

		// check if algorithm returns the right number of containers.
		assertTrue(lu.generateRuns().keySet().size() == numOfNodes);
	}

	// test number of item containers if it matches the number of nodes for
	// bin packing first fit algorithm
	@Test
	public void BBcheckNumberOfBuckets() {
		int numOfNodes = 2;

		BinPackingOrder_FF bb = new BinPackingOrder_FF(numOfNodes, myList, 3);

		// check if algorithm returns the right number of containers.
		assertTrue(bb.generateRuns().keySet().size() == numOfNodes);
	}

	// test number of item containers if it matches the number of nodes for
	// bin packing first fit decreasing algorithm
	@Test
	public void BBDECcheckNumberOfBuckets() {
		int numOfNodes = 2;

		BinPackingOrder_FF_DEC bbdec = new BinPackingOrder_FF_DEC(numOfNodes,
				myList, 3);

		System.out.println(bbdec.generateRuns());
		// check if algorithm returns the right number of containers.
		assertTrue(bbdec.generateRuns().keySet().size() == numOfNodes);
	}

	// check if Bin Packing First Fit decreasing sorts items in the right order
	// (high weight -> low weight)
	@Test
	public void BBDECcheckSort() {
		BinPackingOrder_FF_DEC bbdec = new BinPackingOrder_FF_DEC(2, myList, 4);
		MultiMap myMap = bbdec.generateRuns();
		List firstList = (List) myMap.get(0);
		List secondList = (List) myMap.get(1);

		RunItem item1 = (RunItem) firstList.get(0);
		RunItem item2 = (RunItem) secondList.get(0);

		assertTrue(item1.getWeight() > item2.getWeight());

	}

	// check least used if outputting to the least used node
	@Test
	public void LUcheckOrder() {
		TestCase test3 = new TestCase("test3", 1);
		myList.add(test3);
		LeastUsedOrder lu = new LeastUsedOrder(2, myList);

		List myList = (List) lu.generateRuns().get(0);
		assertTrue(myList.contains(test3));
	}

}
